<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ishowshao
 * Date: 12-10-30
 * Time: 下午1:32
 * To change this template use File | Settings | File Templates.
 */

function parsePostMetaLayout($value) {
    $value = trim($value);
    if ($value !== 'post' && $value !== 'page') {
        $value = 'post';
    }
    return $value;
}

function parsePostMetaDate($value) {
    date_default_timezone_set('PRC');
    return strtotime($value);
}

function parsePostMetaUrl($value) {
    return str_replace(' ', '-', trim($value));
}

function parsePostMetaTitle($value) {
    return trim($value);
}

function parsePostMetaCategories($value) {
    $categories = array();
    $values = explode(',', $value);
    foreach ($values as $value) {
        $value = trim($value);
        if ($value !== '') {
            array_push($categories, $value);
        }
    }
    return $categories;
}

function parsePostMetaTags($value) {
    return parsePostMetaCategories($value);
}

function parsePostMetaAuthor($value) {
    return trim($value);
}

function parsePostMeta($file) {
    $content = file_get_contents($file);
    $parts = explode('---', $content);
    $metaArray = array();
    if (trim($parts[0]) === '') {
        $tempArray = explode("\n", $parts[1]);
        foreach ($tempArray as $item) {
            $item = trim($item);
            if ($item !== '') {
                $meta = explode(':', $item, 2);
                $key = trim($meta[0]);
                $value = $meta[1];
                $functionName = 'parsePostMeta' . ucfirst($key);
                if (function_exists($functionName)) {
                    $metaArray[$key] = $functionName($value);
                }
            }
        }
    }
    return $metaArray;
}
